<?PHP
/*
 * $Id: HashMapTest.class.php 156 2009-12-16 15:59:00Z namba0219 $
 * =============================================================================
 * Barge - Web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\utils;

use \Exception;
use \UnitTestCase;

use barge\utils\ArrayList;
use barge\utils\HashMap;
use barge\utils\IndexOutOfBoundsException;
use barge\utils\IllegalAccessException;
use barge\utils\IllegalArgumentException;

/**
 * @see UnitTestCase
 */
class HashMapTest extends UnitTestCase
{
	/**
	 * @test
	 */
	public function test__construct()
	{
		$map = new HashMap();
		$this->assertIdentical($map->size(), 0);
		$map = new HashMap(array('apple' => 0, 'banana' => 1));
		$this->assertIdentical($map->size(), 2);
		$this->assertIdentical($map->get('apple'), 0);
		$this->assertIdentical($map->get('banana'), 1);
		$map = new HashMap($map);
		$this->assertIdentical($map->size(), 2);
		$this->assertIdentical($map->get('apple'), 0);
		$this->assertIdentical($map->get('banana'), 1);
		$map = new HashMap(new ArrayList(1, 2));
		$this->assertIdentical($map->size(), 2);
		$this->assertIdentical($map->get(0), 1);
		$this->assertIdentical($map->get(1), 2);
		try {
			$map = new HashMap(1);
			$this->fail();
		} catch (IllegalArgumentException $e) {}
	}

	/**
	 * @test
	 */
	public function testSize()
	{
		$map = new HashMap();
		$map->put('one', 1);

		// #size
		$this->assertIdentical($map->size(), 1);
		$this->assertIdentical(count($map), 1);
		$this->assertIdentical($map->count(), 1);
	}

	/**
	 * @test
	 */
	public function testGet()
	{
		$map = new HashMap(array('banana' => 1, 'apple' => 2, 'orenge' => 3));
		$this->assertIdentical($map->get('banana'), 1);
		$this->assertIdentical($map->get('mikan'), null);
	}

	/**
	 * @test
	 */
	public function testPut()
	{
		$map = new HashMap(array('banana' => 1, 'apple' => 2, 'orenge' => 3));
		$this->assertIdentical($map->put('mikan', 4), $map);
		$this->assertIdentical($map->get('apple'), 2);
		$map->put('apple', 100);
		$this->assertIdentical($map->get('apple'), 100);
		$this->assertIdentical($map->get('dlian'), null);
		$map->put('dlian', 5);
		$this->assertIdentical($map->get('dlian'), 5);
	}

	/**
	 * @test
	 */
	public function testIsEmpty()
	{
		$map = new HashMap();
		$this->assertTrue($map->isEmpty());
		$map->put('apple', 1);
		$this->assertFalse($map->isEmpty());
	}

	/**
	 * @test
	 */
	public function testContains()
	{
		$map = new HashMap();
		$this->assertFalse($map->contains('apple'));
		$map->put('apple', 1);
		$this->assertTrue($map->contains('apple'));
		$map->put('1', 2);
		$this->assertTrue($map->contains('1'));
		$this->assertTrue($map->contains(1));
		$map->put(1, 3);
		$this->assertTrue($map->contains('1'));
		$this->assertTrue($map->contains(1));
	}

	/**
	 * @test
	 */
	public function testKeys()
	{
		$map = new HashMap();
		$map->put('apple', 1);
		$map->put('banana', 2);
		$map->put('orange', 3);
		$keys = $map->keys();
		$this->assertIdentical($keys->size(), 3);
		$this->assertIdentical($keys->get(0), 'apple');
		$this->assertIdentical($keys->get(1), 'banana');
		$this->assertIdentical($keys->get(2), 'orange');
	}

	/**
	 * @test
	 */
	public function testToArray()
	{
		$map = new HashMap();
		$map->put('apple', 1);
		$map->put('banana', 2);
		$map->put('orange', 3);
		$array = $map->toArray();
		$this->assertTrue(is_array($array));
		$this->assertTrue(isset($array['apple']) && $array['apple'] === 1);
		$this->assertTrue(isset($array['banana']) && $array['banana'] === 2);
		$this->assertTrue(isset($array['orange']) && $array['orange'] === 3);
	}

	/**
	 * @test
	 */
	public function testValues()
	{
		$map = new HashMap();
		$map->put('apple', 1);
		$map->put('banana', 2);
		$map->put('orange', 3);
		$keys = $map->values();
		$this->assertIdentical($keys->size(), 3);
		$this->assertIdentical($keys->get(0), 1);
		$this->assertIdentical($keys->get(1), 2);
		$this->assertIdentical($keys->get(2), 3);
	}

//	/**
//	 * @test
//	 */
//	public function testArrayIndex()
//	{
//		$map = new HashMap();
//		$map->put('one', 1);
//		$map->put('two', 2);
//		$this->assertTrue($map['one']);
//		$this->assertTrue($map['two']);
//		try {
//			echo $map[2];
//			$this->fail();
//		} catch (IndexOutOfBoundsException $e) {}
//		// isset
//		$this->assertTrue(isset($map[0]));
//		$this->assertTrue(isset($map[2]) === false);
//		// unset
//		try {
//			unset($map[2]);
//			$this->fail();
//		} catch (IllegalAccessException $e) {}
//	}


}

?>